import bisect
class Solution(object):
    def platesBetweenCandles(self, s, queries):
        n = len(s)
        prefix = [0] * n
        c_list = []
        if s[0] == '|':
            prefix[0] = 1
            c_list.append(0)
        for i in range(1, n):
            if s[i] == '|':
                prefix[i] = prefix[i - 1] + 1
                c_list.append(i)
            else:
                prefix[i] = prefix[i - 1]
        ans = []
        for s, e in queries:
            left_index = bisect.bisect_left(c_list, s)
            right_index = bisect.bisect_right(c_list, e)
            if right_index - left_index < 1:
                ans.append(0)
            else:
                left_c = c_list[left_index]
                right_c = c_list[right_index - 1]
                ans.append(right_c - left_c - (prefix[right_c] - prefix[left_c]))
        return ans
